import { getVersion } from "src/services/mainService";

export async function checkVersion() {
    try {
        const res = await getVersion();
        console.log(res.data);
        
        const version = res?.data.version;
        const commit_hash = process.env.COMMIT_HASH

        console.log(`local version: ${commit_hash}, server version: ${version}`)

        const currentCheck = `${commit_hash} === ${version}`
        const lastChecked = localStorage.getItem('lastChecked');
        const firstCheck = lastChecked != currentCheck
        const versionMatch = commit_hash === version;
        localStorage.setItem('lastChecked', currentCheck);

        if (versionMatch){
            return true
        }

        if (firstCheck) {
            return false
        }

        return true;
    } catch (err) {
        console.log('Check version fail');
        return false;
    }
}

export const VERSION = process.env.COMMIT_HASH || ''